package com.ds.infrastructure.hyperspace.container.internal.http2;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import lombok.Getter;

/**
 * @author  Weishaoying
 */
@Getter
public class HyperspaceHttp2FrameWrapper {

    private HyperspaceHttp2FrameTypeEnum frameType;

    private int streamId = 0;

    private boolean endOfStream;

    private Object frameData;

    private HyperspaceHttp2FlowControllerState flowControllerState;

    public HyperspaceHttp2FrameWrapper(HyperspaceHttp2FrameTypeEnum frameType, Object frameData) {
        this.frameType = frameType;
        this.frameData = frameData;
    }

    public HyperspaceHttp2FrameWrapper(HyperspaceHttp2FrameTypeEnum frameType, int streamId, Object frameData) {
        this.frameType = frameType;
        this.streamId = streamId;
        this.frameData = frameData;
    }

    public HyperspaceHttp2FrameWrapper(HyperspaceHttp2FrameTypeEnum frameType, int streamId,
            boolean endOfStream, Object frameData) {
        this.frameType = frameType;
        this.streamId = streamId;
        this.endOfStream = endOfStream;
        this.frameData = frameData;
    }

    public HyperspaceHttp2FrameWrapper(HyperspaceHttp2FrameTypeEnum frameType, int streamId,
            HyperspaceHttp2FlowControllerState flowControllerState, Object frameData) {
        this.frameType = frameType;
        this.streamId = streamId;
        this.flowControllerState = flowControllerState;
        this.frameData = frameData;
    }

    public HyperspaceHttp2FrameWrapper(HyperspaceHttp2FrameTypeEnum frameType, int streamId, boolean endOfStream,
            HyperspaceHttp2FlowControllerState flowControllerState, Object frameData) {
        this.frameType = frameType;
        this.streamId = streamId;
        this.endOfStream = endOfStream;
        this.flowControllerState = flowControllerState;
        this.frameData = frameData;
    }

    public int payloadSize() {
        if (frameData instanceof ByteBuf) {
            return ((ByteBuf) frameData).readableBytes();
        } else if (frameData instanceof ByteBufHolder) {
            return ((ByteBufHolder) frameData).content().readableBytes();
        }
        return 1;
    }

}
